﻿
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using RBS.DBUtility;//Please add references
namespace RBS.Core.DAL
{
	/// <summary>
	/// 数据访问类:SYS_SMS
	/// </summary>
	public partial class SYS_SMS
	{
		public SYS_SMS()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("SMS_ID", "SYS_SMS"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int SMS_ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from SYS_SMS");
			strSql.Append(" where SMS_ID=@SMS_ID");
			SqlParameter[] parameters = {
					new SqlParameter("@SMS_ID", SqlDbType.Int,4)
			};
			parameters[0].Value = SMS_ID;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(RBS.Core.Model.SYS_SMS model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into SYS_SMS(");
			strSql.Append("CLIENT_TX_SEQ,SENDER_ACCOUNT,BATCHNO,SUBJECTNO,SMS_CONTENT,SENDER,CREATE_TIME,Column_9)");
			strSql.Append(" values (");
			strSql.Append("@CLIENT_TX_SEQ,@SENDER_ACCOUNT,@BATCHNO,@SUBJECTNO,@SMS_CONTENT,@SENDER,@CREATE_TIME,@Column_9)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@CLIENT_TX_SEQ", SqlDbType.VarChar,32),
					new SqlParameter("@SENDER_ACCOUNT", SqlDbType.NVarChar,64),
					new SqlParameter("@BATCHNO", SqlDbType.VarChar,32),
					new SqlParameter("@SUBJECTNO", SqlDbType.NVarChar,200),
					new SqlParameter("@SMS_CONTENT", SqlDbType.NVarChar,4000),
					new SqlParameter("@SENDER", SqlDbType.NVarChar,64),
					new SqlParameter("@CREATE_TIME", SqlDbType.DateTime),
					new SqlParameter("@Column_9", SqlDbType.Char,10)};
			parameters[0].Value = model.CLIENT_TX_SEQ;
			parameters[1].Value = model.SENDER_ACCOUNT;
			parameters[2].Value = model.BATCHNO;
			parameters[3].Value = model.SUBJECTNO;
			parameters[4].Value = model.SMS_CONTENT;
			parameters[5].Value = model.SENDER;
			parameters[6].Value = model.CREATE_TIME;
			parameters[7].Value = model.Column_9;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(RBS.Core.Model.SYS_SMS model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update SYS_SMS set ");
			strSql.Append("CLIENT_TX_SEQ=@CLIENT_TX_SEQ,");
			strSql.Append("SENDER_ACCOUNT=@SENDER_ACCOUNT,");
			strSql.Append("BATCHNO=@BATCHNO,");
			strSql.Append("SUBJECTNO=@SUBJECTNO,");
			strSql.Append("SMS_CONTENT=@SMS_CONTENT,");
			strSql.Append("SENDER=@SENDER,");
			strSql.Append("CREATE_TIME=@CREATE_TIME,");
			strSql.Append("Column_9=@Column_9");
			strSql.Append(" where SMS_ID=@SMS_ID");
			SqlParameter[] parameters = {
					new SqlParameter("@CLIENT_TX_SEQ", SqlDbType.VarChar,32),
					new SqlParameter("@SENDER_ACCOUNT", SqlDbType.NVarChar,64),
					new SqlParameter("@BATCHNO", SqlDbType.VarChar,32),
					new SqlParameter("@SUBJECTNO", SqlDbType.NVarChar,200),
					new SqlParameter("@SMS_CONTENT", SqlDbType.NVarChar,4000),
					new SqlParameter("@SENDER", SqlDbType.NVarChar,64),
					new SqlParameter("@CREATE_TIME", SqlDbType.DateTime),
					new SqlParameter("@Column_9", SqlDbType.Char,10),
					new SqlParameter("@SMS_ID", SqlDbType.Int,4)};
			parameters[0].Value = model.CLIENT_TX_SEQ;
			parameters[1].Value = model.SENDER_ACCOUNT;
			parameters[2].Value = model.BATCHNO;
			parameters[3].Value = model.SUBJECTNO;
			parameters[4].Value = model.SMS_CONTENT;
			parameters[5].Value = model.SENDER;
			parameters[6].Value = model.CREATE_TIME;
			parameters[7].Value = model.Column_9;
			parameters[8].Value = model.SMS_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int SMS_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_SMS ");
			strSql.Append(" where SMS_ID=@SMS_ID");
			SqlParameter[] parameters = {
					new SqlParameter("@SMS_ID", SqlDbType.Int,4)
			};
			parameters[0].Value = SMS_ID;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string SMS_IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from SYS_SMS ");
			strSql.Append(" where SMS_ID in ("+SMS_IDlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_SMS GetModel(int SMS_ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 SMS_ID,CLIENT_TX_SEQ,SENDER_ACCOUNT,BATCHNO,SUBJECTNO,SMS_CONTENT,SENDER,CREATE_TIME,Column_9 from SYS_SMS ");
			strSql.Append(" where SMS_ID=@SMS_ID");
			SqlParameter[] parameters = {
					new SqlParameter("@SMS_ID", SqlDbType.Int,4)
			};
			parameters[0].Value = SMS_ID;

			RBS.Core.Model.SYS_SMS model=new RBS.Core.Model.SYS_SMS();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public RBS.Core.Model.SYS_SMS DataRowToModel(DataRow row)
		{
			RBS.Core.Model.SYS_SMS model=new RBS.Core.Model.SYS_SMS();
			if (row != null)
			{
				if(row["SMS_ID"]!=null && row["SMS_ID"].ToString()!="")
				{
					model.SMS_ID=int.Parse(row["SMS_ID"].ToString());
				}
				if(row["CLIENT_TX_SEQ"]!=null)
				{
					model.CLIENT_TX_SEQ=row["CLIENT_TX_SEQ"].ToString();
				}
				if(row["SENDER_ACCOUNT"]!=null)
				{
					model.SENDER_ACCOUNT=row["SENDER_ACCOUNT"].ToString();
				}
				if(row["BATCHNO"]!=null)
				{
					model.BATCHNO=row["BATCHNO"].ToString();
				}
				if(row["SUBJECTNO"]!=null)
				{
					model.SUBJECTNO=row["SUBJECTNO"].ToString();
				}
				if(row["SMS_CONTENT"]!=null)
				{
					model.SMS_CONTENT=row["SMS_CONTENT"].ToString();
				}
				if(row["SENDER"]!=null)
				{
					model.SENDER=row["SENDER"].ToString();
				}
				if(row["CREATE_TIME"]!=null && row["CREATE_TIME"].ToString()!="")
				{
					model.CREATE_TIME=DateTime.Parse(row["CREATE_TIME"].ToString());
				}
				if(row["Column_9"]!=null)
				{
					model.Column_9=row["Column_9"].ToString();
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select SMS_ID,CLIENT_TX_SEQ,SENDER_ACCOUNT,BATCHNO,SUBJECTNO,SMS_CONTENT,SENDER,CREATE_TIME,Column_9 ");
			strSql.Append(" FROM SYS_SMS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" SMS_ID,CLIENT_TX_SEQ,SENDER_ACCOUNT,BATCHNO,SUBJECTNO,SMS_CONTENT,SENDER,CREATE_TIME,Column_9 ");
			strSql.Append(" FROM SYS_SMS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM SYS_SMS ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.SMS_ID desc");
			}
			strSql.Append(")AS Row, T.*  from SYS_SMS T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "SYS_SMS";
			parameters[1].Value = "SMS_ID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

